package net.xxcl.exam.controller;

import net.xxcl.exam.pojo.Criteria;
import net.xxcl.exam.pojo.Paper;
import net.xxcl.exam.pojo.Student;
import net.xxcl.exam.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * User: 夏汛
 * Date: 11-12-25
 * Time: 上午11:22
 */
@Controller
@RequestMapping("/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;

    /**
     * 获取所有试卷
     * @param start
     * @param limit
     * @return
     */
    @RequestMapping(value = "/papers",method= RequestMethod.POST)
    public @ResponseBody
    Map<String,Object> getPapers(@RequestParam("start") Integer start, @RequestParam("limit") Integer limit){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        int total = paperService.countByExample(criteria);
        criteria.setMysqlOffset(start);
        criteria.setMysqlLength(limit);
        criteria.setOrderByClause("id desc");
        List<Paper> list = paperService.selectByExample(criteria);
        responseMap.put("total",total);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 新增试卷
     * @param paper
     * @return
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public @ResponseBody Map<String,Object> save(@ModelAttribute("paper") Paper paper,
                                                 @RequestParam(value = "danxuan",required = false) Integer danxuan,
                                                 @RequestParam(value = "duoxuan",required = false) Integer duoxuan,
                                                 @RequestParam(value = "panduan",required = false) Integer panduan,
                                                 @RequestParam(value = "wordCaozuo",required = false) Integer wordCaozuo,
                                                 @RequestParam(value = "excelCaozuo",required = false) Integer excelCaozuo,
                                                 @RequestParam(value = "powerpointCaozuo",required = false) Integer powerpointCaozuo,
                                                 @RequestParam(value = "dazi",required = false) Integer dazi){
        Map<String,Object> responseMap = new HashMap<String,Object>();System.out.println(paper.getStarttime());
        if(danxuan==null && danxuan==null && panduan==null && wordCaozuo==null && excelCaozuo==null && powerpointCaozuo==null && dazi==null){
            responseMap.put("info", "增加失败，你没有设置题目数量！");
            return responseMap;
        }
        try{
            paperService.insertPaper(paper,danxuan,duoxuan,panduan,wordCaozuo,excelCaozuo,powerpointCaozuo,dazi);
            responseMap.put("method", "Create");
            responseMap.put("success", "true");
            responseMap.put("info", "新增成功！");
        } catch (Exception e){
            e.printStackTrace();
            responseMap.put("info", e.getMessage());
        }
        return responseMap;
    }

    /**
     * 删除试卷
     * @param ids
     * @return
     */
    @RequestMapping(value="/remove",method=RequestMethod.POST)
    public @ResponseBody Map<String,Object> remove(@RequestParam("ids") String ids){
        Map<String,Object> responseMap = new HashMap<String,Object>();
        try {
            paperService.deletePaper(ids);
            responseMap.put("success", "true");
            responseMap.put("info", "删除成功！");
            return responseMap;
        }catch(Exception e) {
            e.printStackTrace();
            responseMap.put("info", e.getMessage());
            return responseMap;
        }
    }

    /**
     * 获取考生能够参加考试的试卷
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getPaperforKs")
    public @ResponseBody
    Map<String,Object> getPaperforKs(HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        Student student = (Student)request.getSession().getAttribute("student");
        criteria.put("studentid",student.getId());
        criteria.setOrderByClause("id desc");
        List<Paper> list = paperService.getPaperforKs(criteria);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 获取考生在查看成绩栏能看到的试卷
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getPaperforLook")
    public @ResponseBody
    Map<String,Object> getPaperforLook(HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        Student student = (Student)request.getSession().getAttribute("student");
        criteria.put("studentid",student.getId());
        List<Paper> list = paperService.getPaperforLook(criteria);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 获取所有未结束的考试
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/getNotEndPaper")
    public @ResponseBody
    Map<String,Object> getNotEndPaper(HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        Criteria criteria = new Criteria();
        Student student = (Student)request.getSession().getAttribute("student");
        List<Integer> notEnd = new ArrayList<Integer>();
        notEnd.add(1);  //1为已通过审核
        notEnd.add(2);  //2为已开始考试
        criteria.put("notEnd",notEnd);
        criteria.setOrderByClause("id desc");
        List<Paper> list = paperService.selectByExample(criteria);
        responseMap.put("rows",list);
        return responseMap;
    }

    /**
     * 改变试卷状态
     * @param id
     * @param state
     * @return
     */
    @RequestMapping(value = "/changePaper")
    public @ResponseBody Map<String,Object> changePaper(@RequestParam("id") Integer id,@RequestParam("state") Integer state){
        Map<String,Object> responseMap = new HashMap<String, Object>();
        try{
            Paper paper = new Paper();
            if(state==1){
                paper = paperService.selectByPrimaryKey(id);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                if(!sdf.format(new Date()).equals(sdf.format(paper.getStarttime()))){
                    responseMap.put("info","服务器当前日期为："+sdf.format(new Date())+
                        "，不是试卷设定的考试日期");
                    return responseMap;
                }
            }
            paper.setId(id);
            paper.setState(state+1);
            paperService.updateByPrimaryKeySelective(paper);
            responseMap.put("info","设置成功");
        } catch (Exception e){
            responseMap.put("info",e.getMessage());
        }
        return responseMap;
    }
}
